<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <title>Insert title here</title>
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
    <link rel="stylesheet" href="../../css/global.css" media="all"/>
</head>
<body>
<div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <div>
            <header class="form-filter-wrap">
                <header class="form-filter-wrap">
                    <form class="layui-form form-inline" onsubmit="return false">
                        <div class="form-group">
                            <div class="form-filter">
                                <div class="form-inner">
                                    <label class="layui-form-label" for="">单位：</label>
                                    <div class="layui-input-inline">
                                        <select multiple lay-omit id="branch" lay-search>
                                            <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-inner">
                                    <label class="layui-form-label" for="">巡察年度：</label>
                                    <div class="layui-input-inline">

                                        <select multiple lay-omit id="startPatrolTime">
                                            <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-inner">
                                    <label class="layui-form-label" for="">问题类别：</label>
                                    <div class="layui-input-inline">
                                        <select multiple lay-omit id="questionType">
                                            <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <button id="searchBt" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe615;</i>搜索
                                </button>
                            </div>
                            <div class="form-btn">
                                <button type="button" class="layui-btn layui-btn-sm" id="test4"><i
                                        class="layui-icon"></i>查看附件列表
                                </button>
                            </div>
                        </div>

                    </form>
                </header>
            </header>

            <div>
                <div class="widget-body no-padding">
                    <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                        <thead>
                        <tr>
                            <th></th>
                            <th>问题项</th>
                            <th>单位</th>
                            <th>问题类型</th>
                            <th>录入时间</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<!--附件下载-->
<div id="hidden1" lay-filter="hidden1" style="display: none">
    <table id="department_result" lay-filter="department_result"></table>
</div>
<div id="openProductBox2" style="display: none; padding: 10px;">
    <button type="button" lay-event="del" class="layui-btn layui-btn-sm" id="down"><i class="layui-icon"></i>查看附件详情</button>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<!--删除按钮-->
<script type="text/html" id="itemTpl">
    <button type="button" lay-event="del1" class="layui-btn layui-btn-sm" id="del1"><i class="layui-icon"></i>删除
    </button>
</script>

<script type="text/javascript" src="../../js/libs/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/dict.js"></script>
<script type="text/javascript">
    /**
     * 发起督办问题行为
     */
    $('#btnLaunchProcess').click(function () {
        formOpen()
    })

    layui.use('layer', function () {
        var layer = layui.layer;
    });
    /*公司*/
    layui.use(['layer', 'form', 'jquery'], function () {
        var form = layui.form;
        $.ajax({
            url: '/dropDown/getBranch',
            dataType: 'json',
            type: 'post',
            success: function (data) {
                // 使用循环遍历，给下拉列表赋值
                $.each(data, function (index, value) {
                    $('#branch').append(new Option(value.value, value.key));// 下拉菜单里添加元素
                });
                form.render();//重新渲染 固定写法
            }
        })
            /*年度*/
            , $.ajax({
            url: '/dropDown/getYear',
            dataType: 'json',
            type: 'post',
            success: function (data) {
                // 使用循环遍历，给下拉列表赋值
                $.each(data, function (index, value) {
                    $('#startPatrolTime').append(new Option(value.value, value.value));// 下拉菜单里添加元素
                });
                form.render();//重新渲染 固定写法
            }
        })
            ,
            /*问题类别*/
            $.ajax({
                url: '/dropDown/getQuestion',
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    // 使用循环遍历，给下拉列表赋值
                    $.each(data, function (index, value) {
                        $('#questionType').append(new Option(value.value, value.value));// 下拉菜单里添加元素
                    });
                    form.render();//重新渲染 固定写法
                }
            })


    });
    var pers = checkPermission();

    var example;

    function init() {
        example =
            $('#dt-table').DataTable({
                "searching": false,
                "processing": false,
                "serverSide": true,
                "ordering": false,
                "language": {
                    "url": "/js/plugin/datatables/Chinese.lang"
                },
                "ajax": {
                    "url": "/mOverseeQuestions/draftsList",
                    "type": "get",
                    "data": function (d) {
                        if ($("#questionType").val() != null) {
                            d.questionType = JSON.stringify($("#questionType").val());
                        }
                        if ($("#startPatrolTime").val() != null) {
                            d.startPatrolTime = JSON.stringify($("#startPatrolTime").val());
                        }
                        if ($("#branch").val() != null) {
                            d.branchId = JSON.stringify($("#branch").val());
                        }
                    },
                    "error": function (xhr, textStatus, errorThrown) {
                        var msg = xhr.responseText;
                        console.log(msg);
                        var response = JSON.parse(msg);
                        var code = response.code;
                        var message = response.message;
                        if (code == 400) {
                            layer.msg(message);
                        } else if (code == 401) {
                            localStorage.removeItem("token");
                            layer.msg("token过期，请先登录", {shift: -1, time: 1000}, function () {
                                location.href = '/login.html';
                            });
                        } else if (code == 403) {
                            console.log("未授权:" + message);
                            layer.msg('未授权');
                        } else if (code == 500) {
                            layer.msg('系统错误：' + message);
                        }
                    }
                },
                "dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'table-bottom col-sm-10 col-xs-12 hidden-xs'i><'table-bottom col-xs-12 col-sm-10' p v>>",
                "scrollX": true,
                "fixedHeight": 300,
                "bAutoWidth": true,
                "columns": [
                    {
                        "data": "id",
                        "render": function (data, type, row, meta) {
                            return "<input type='radio' class='table-checkbox'  name='childclick' id='' value=" + data + " />";
                        }
                    },
                    {"data": "question", "defaultContent": ""},
                    {"data": "branch", "defaultContent": ""},
                    {"data": "questionType", "defaultContent": ""},
                    {"data": "createTime", "defaultContent": ""},
                    {
                        "data": "",
                        "defaultContent": "",
                        "orderable": false,
                        "render": function (data, type, row) {
                            var id = row['id'];
                            var edit = buttonOpen(id, "", pers);
                            var del = buttonDel(id, "", pers);
                            return edit + del;
                        }
                    },
                ],
                "order": [[0, "asc"]]
            });
    }

    function del(id) {
        layer.confirm('<div class="layui-icon" style="text-align: center">确认删除？</div>', {
            title: '<div class="layui-icon" style="font-weight: bold">删除问题草稿</div>',
            skin: "my-skin",
            btn: ['确定', '取消'],
            success: function (layero) {
                layero.find('.layui-layer-btn').css('text-align', 'center')
            },
        }, function () {
            $.ajax({
                type: 'delete',
                url: '/mOverseeQuestions/' + id,
                success: function (data) {
                    example.ajax.reload();
                    layer.msg('删除成功');
                }
            });

            layer.close(1);
        });
    }

    $("#test4").click(function () {

        layui.use(['table', 'form'], function () {

            var table = layui.table;


            var id = $('input[type="radio"]:checked').val()

            console.log($('input[type="radio"]:checked').val())

            var form = layui.form;

            if (id.length > 0) {

                $.ajax({
                    url: '/mOverseeQuestions/fileList',
                    dataType: 'json',
                    type: 'post',
                    data: {id: id},
                    success: function (data) {
                        if (data.count === 0) {
                            layer.msg('未上传附件');
                        } else {
                            layer.open({
                                type: 1,
                                title: '<div class="layui-icon" style="font-weight: bold;text-align: center"> 附件列表</div>',
                                area: ['80%', '80%'], //宽高
                                content: $('#openProductBox2'),
                                success: function (data) {

                                    table.render({
                                        elem: '#test',
                                        width: '100%'
                                        , method: 'post', //接口http请求类型，默认：get
                                        page: false,
                                        limit: Number.MAX_VALUE,
                                        url: '/mOverseeQuestions/fileList', //?page=1&limit=10（该参数可通过 request 自定义）
                                        where: {id: id},
                                        cols: [[
                                            {
                                                type: 'checkbox'
                                            }, {
                                                field: 'branch', //字段名
                                                title: '单位名称', //标题
                                                width: 200
                                                //fixed: 'left' //固定列
                                            }, {
                                                field: 'question', //字段名
                                                title: '问题项', //标题
                                                width: 260
                                            }, {
                                                field: 'path', //字段名
                                                title: '附件名称', //标题
                                                width: 454
                                            }, {
                                                field: '', title: '操作',
                                                width: 80,
                                                toolbar: '#itemTpl',
                                                fixed: 'right' //固定列
                                            }]
                                        ],
                                        done: function (res, curr, count) {
                                            $('th').css({'font-weight': 'bold'});
                                        }
                                    });
                                }
                            });
                        }
                    }
                })

            }
        });
        layui.use('table', function () {
            var table = layui.table;

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var checkStatus = layui.table.checkStatus('openProductBox2').data;
                var data = obj.data;
                //console.log(data)
                if (obj.event === 'del') {
                    var s = data.path;
                    location.href = '/mOverseeQuestions/downSource?path=' + s;
                }
                if (obj.event === 'del1') {
                    var id = data.id;
                    $.ajax({
                        url: '/mOverseeQuestions/deletePathDrafs?id=' + id,
                        type: 'get',
                        success: function () {
                            layer.msg("删除成功", {
                                icon: 1,
                                time: 1000
                            });
                            table.reload("test");
                        }
                    })
                }

            });
        });

        // 附件下载
        function sleep(n) {
            var start = new Date().getTime();//定义起bai始时间的毫秒数
            while (true) {
                var time = new Date().getTime();//每次执行循环取得du一次当前时间的毫秒数
                if (time - start > n) {//如果当前时间的毫秒数减去起始时间的毫秒数大于给定的毫秒数，即结束循环
                    break;
                }
            }
        }

        function aa() {
            var ids = [];
            $.each($('input[name=\'childclick\']:checkbox'), function () {
                if (this.checked) {
                    ids.push($(this).val())
                }
            });
            if (ids.length === 0) {
                layer.msg('请选择数据查看');
                return
            }

            var url = "/mOverseeQuestions/downSource?id=" + encodeURI(JSON.stringify(ids));
            $.fileDownload(url, {
                httpMethod: 'GET',
                //同时上传表单数据
                data: '',
                prepareCallback: function (url) {
                    $('#down').attr('disabled', false)
                    console.log("请稍等！");
                },
                successCallback: function (url) {

                    $('#down').attr('disabled', false)
                    return false
                },
                failCallback: function (html, url) {
                    var json = JSON.parse(html);
                    console.log(json);
                    $('#down').attr('disabled', false)
                }
            });
            return false
        }
        $('#down').on('click',  function () {
            aa()
        })
    })

    // 发起修改
    function edit(id) {
        window.open('overseeQuestionPanel.html?id=' + id, 'toolbar =no, menubar=no')
    }

    var questionId = '';


    $("#searchBt").click(function () {
        example.ajax.reload();
    });

    init();


</script>
